<?php
/**
 * AuthBase 是需要登录的时候继承就可以
 * Created by singwa
 * User: singwa
 * motto: 现在的努力是为了小时候吹过的牛逼！
 * Time: 12:27
 */
namespace app\api\controller;

class AuthBase extends ApiBase {
    public $userId = 0;
    public $username = "";
    public $accessToken = "";
    public $isLogin = 1;
    public function initialize()
    {

        // 关于登录 小伙伴其实可以仿照之前老师讲解的admin 下登录中间件来完善下
        parent::initialize(); // TODO: Change the autogenerated stub
        if($this->isLogin == 1) {
            $this->userId = 9; // 测试场景
            return true;
        }
        $this->accessToken = $this->request->header("access-token");
        if(!$this->accessToken || !$this->isLogin()) {
            return $this->show(config("status.not_login"), "没有登录");
        }

    }

    /**
     * 判断用户是否登录
     * @return bool
     */
    public function isLogin() {
        $userInfo = cache(config("redis.token_pre").$this->accessToken);
        if(!$userInfo) {
            return false;
        }
        if(!empty($userInfo['id']) && !empty($userInfo['username'])) {
            $this->username = $userInfo['username'];
            $this->userId = $userInfo['id'];
            return true;
        }

        return false;

    }
}